import 'package:flutter/material.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:photo_view/photo_view.dart';

class EcPreviewPhotoViewPage extends StatefulWidget {
  EcPreviewPhotoViewPage(this.urls, {Key key, this.currentIndex = 1}) : super(key: key);

  //图片的链接
  final List<String> urls;

  //当前的url
  final int currentIndex;
  @override
  _EcPreviewPhotoViewPageState createState() => _EcPreviewPhotoViewPageState();
}

///预览图片
const String PREVIEW_IMAGE_PAGE = '/preview_image_page';

class _EcPreviewPhotoViewPageState extends State<EcPreviewPhotoViewPage> {
  int _currentIndex;

  PageController _pageController;
  @override
  void initState() {
    _currentIndex = widget.currentIndex;
    _pageController = PageController(initialPage: _currentIndex);
    super.initState();
  }

  void onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
    final String item = widget.urls[index];
    return PhotoViewGalleryPageOptions(
        imageProvider: NetworkImage(item),
        initialScale: PhotoViewComputedScale.contained,
        minScale: PhotoViewComputedScale.contained * (0.5 + index / 10),
        maxScale: PhotoViewComputedScale.covered * 1.1,
        onTapUp: (BuildContext context, TapUpDetails details, PhotoViewControllerValue controllerValue) {
          Navigator.of(context).pop();
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        alignment: Alignment.bottomRight,
        children: [
          PhotoViewGallery.builder(
              itemCount: widget.urls.length,
              builder: _buildItem,
              onPageChanged: onPageChanged,
              pageController: _pageController,
              backgroundDecoration: BoxDecoration(color: Colors.black)),
          Container(
            padding: const EdgeInsets.all(30.0),
            child: Text(
              "${_currentIndex + 1}/${widget.urls.length}",
              style: const TextStyle(color: Colors.white, fontSize: 17.0, decoration: null),
            ),
          ),
          Positioned(
              top: 40,
              left: 25,
              child: Container(
                child: IconButton(
                  icon: Icon(
                    Icons.arrow_back_ios,
                    size: 25,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ))
        ],
      ),
      //   alignment: Alignment.bottomRight,
      //   children: <Widget>[
      //     PhotoViewGallery.builder(
      //       scrollPhysics: const BouncingScrollPhysics(),
      //       builder: _buildItem,
      //       itemCount: galleryItems.length,
      //       loadingChild: widget.loadingChild,
      //       backgroundDecoration: widget.backgroundDecoration,
      //       pageController: widget.pageController,
      //       onPageChanged: onPageChanged,
      //       usePageViewWrapper: widget.usePageViewWrapper,
      //     ),
    );
  }
}

// class GalleryPhotoViewWrapper extends StatefulWidget {
//   GalleryPhotoViewWrapper(
//       {this.loadingChild,
//       this.usePageViewWrapper = false,
//       this.backgroundDecoration,
//       this.minScale,
//       this.maxScale,
//       this.initialIndex,
//       @required this.galleryItems})
//       : pageController = PageController(initialPage: initialIndex);

//   final Widget loadingChild;
//   final Decoration backgroundDecoration;
//   final dynamic minScale;
//   final dynamic maxScale;
//   final int initialIndex;
//   final PageController pageController;
//   final List<GalleryExampleItem> galleryItems;
//   final bool usePageViewWrapper;

//   @override
//   State<StatefulWidget> createState() {
//     return _GalleryPhotoViewWrapperState();
//   }
// }

// class _GalleryPhotoViewWrapperState extends State<GalleryPhotoViewWrapper> {
//   int currentIndex;

//   @override
//   void initState() {
//     currentIndex = widget.initialIndex;
//     super.initState();
//   }

//   void onPageChanged(int index) {
//     if (!widget.usePageViewWrapper) {
//       setState(() {
//         currentIndex = index;
//       });
//     }
//   }
// }
